package com.changgou.web.controller;

import com.changgou.entity.Page;
import com.changgou.search.feign.SkuInfoFeign;
import com.changgou.search.pojo.SkuInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;
import java.util.Set;

/**
 * @ClassName SearchController
 * @Description 检索
 * @Author 洛尘
 * @Date 16:43 2019/11/18
 * @Version 2.1
 **/
@Controller
@RequestMapping("/search")
public class SearchController {

    @Autowired
    private SkuInfoFeign skuInfoFeign;

    @GetMapping("/list")
    public String list(@RequestParam(required = false) Map<String, String> searchMap, Model model) {
        //替换特殊字符
        handlerSearchMap(searchMap);

        Map<String, Object> result = skuInfoFeign.search(searchMap);
        //  检索的数据
        model.addAttribute("search", searchMap);
        //  返回的数据
        model.addAttribute("result", result);
        //组装url
        String[] urls = getUrl(searchMap);
        model.addAttribute("url", urls[0]);
        model.addAttribute("sorturl",urls[1]);

        long total = Long.parseLong(result.get("totalPages").toString());
        int currentpage = Integer.parseInt(result.get("pageNum").toString());
        int pagesize = Integer.parseInt(result.get("pageSize").toString());
        //分页计算
        Page<SkuInfo> page = new Page<>(total, currentpage+1, pagesize);
        model.addAttribute("page", page);

        return "search";
    }

    /**
     * 对于url的升序或者降序，都是对于url的重新拼接
     *                  URL请求地址的处理
     * @param searchMap
     * @return
     */
    private String[] getUrl(Map<String, String> searchMap) {
        String url = "/search/list";
        String sorturl = "/search/list";
        if (searchMap != null && searchMap.size() > 0) {
            url += "?";
            sorturl += "?";
            Set<Map.Entry<String, String>> entries = searchMap.entrySet();
            for (Map.Entry<String, String> entry : entries) {
                String key = entry.getKey();
                if (key.equals("pageNum")) {
                    continue;
                }
                url += key + "=" + entry.getValue() + "&";
                if(key.equals("sortFiled") || key.equals("sortRule")){
                    continue;
                }
                sorturl += key + "=" + entry.getValue() +"&";
            }
            url = url.substring(0, url.length() - 1);
            sorturl = sorturl.substring(0,sorturl.length() -1);
        }

        return new String[]{url,sorturl};
    }

    /**
     * 替换普通字符
     * @param searchMap
     */
    private void handlerSearchMap(Map<String,String> searchMap){
        if (searchMap != null && searchMap.size()>0){
            Set<Map.Entry<String, String>> entries = searchMap.entrySet();
            for (Map.Entry<String, String> entry : entries) {
                if(entry.getValue().startsWith("spec_")){
                    entry.setValue(entry.getValue().replace("+","%2B"));
                }
            }
        }
    }

}
